class_name GameManager extends Node2D

var pre_small = preload("res://游戏/core/小子/Small.tscn")
var pre_big = preload("res://游戏/core/大子/Big.tscn")
var pre_幽灵 = preload("res://游戏/core/大子/转型/幽灵/幽灵.tscn")
var pre_恶灵 = preload("res://游戏/core/大子/转型/恶灵/恶灵.tscn")
var pre_炮台 = preload("res://游戏/core/大子/转型/炮台/炮台.tscn")
var pre_狙炮 = preload("res://游戏/core/大子/转型/狙炮/狙炮.tscn")
var pre_顽石 = preload("res://游戏/core/大子/转型/顽石/顽石.tscn")
var pre_地灵 = preload("res://游戏/core/大子/转型/地灵/地灵.tscn")
var pre_精灵 = preload("res://游戏/core/大子/转型/精灵/精灵.tscn")
var pre_沙兵 = preload("res://游戏/core/大子/转型/沙兵/沙兵.tscn")
var pre_沙皇 = preload("res://游戏/core/大子/转型/沙皇/沙皇.tscn")
var pre_野猪 = preload("res://游戏/core/大子/转型/野猪/野猪.tscn")
var pre_野马 = preload("res://游戏/core/大子/转型/野马/野马.tscn")
var pre_拳师 = preload("res://游戏/core/大子/转型/拳师/拳师.tscn")
var pre_灵阵 = preload("res://游戏/core/大子/转型/灵阵/灵阵.tscn")
var pre_天谕 = preload("res://游戏/core/大子/转型/天谕/天谕.tscn")
var pre_妖术 = preload("res://游戏/core/大子/转型/妖术/妖术.tscn")
var pre_鼓舞 = preload("res://游戏/core/大子/转型/鼓舞/鼓舞.tscn")

@onready var ui:UI = $"UI"
@onready var camera:Camera2D = $"战场/Camera2D"
@onready var layer_chesses = $"战场/棋子层"
@onready var layer_display = $"战场/展示层"
@onready var layer_jian = $"战场/键线层"
@onready var layer_effect = $"战场/特效层"


var operationManager = OperationManager.new()
var handlerManager: HandlerManager

var prefab_point:PackedScene = preload("res://游戏/core/落点/Point.tscn")
var map_points = Dictionary()
var map_players = Dictionary()
var turn_player:Player
var me_player:Player		#这个对象为空说明是观战、复盘模式
var is_gameOver = false		#是否游戏已经结束了


func _init():
	ObjectManager.gameManager = self
	handlerManager = HandlerManager.new(self)

func _ready():
	operationManager.is_can_click_point = false
	# 等4s
	await get_tree().create_timer(4).timeout
	operationManager.is_can_click_point = true
	if me_player:
		operationManager.selectedPoint = me_player.small.point
	operationManager.is_can_control_camera = true
	$"战场/视野刷新定时器".start(0.05)

func init_players(data:Dictionary):
	var list = data["playerList"]
	var num = 0
	for obj in list:
		var player:Player = Player.new()
		player.init(obj,num)
		map_players.merge({obj.username:player})
		num += 1

func init_map(data:Dictionary):
	var list = data["pointList"]
	for point in list:
		var node:Point = prefab_point.instantiate()
		$"战场/棋盘层".add_child(node)
		node.init(point)

# 刷新视野
func refreshView():
	if is_gameOver:
		return
	var arr_points = GameUtils.getArrPointsHasView()
	for point in map_points.values():
		point.is_view = false
	for point in arr_points:
		point.is_view = true
	for point in map_points.values():
		point.refreshView()
	# 大子显示刷新
	for player in map_players.values():
		for big in player.map_bigs.values():
			if big == null:
				continue
			big.updateVisible()
	# 大子的键显示刷新
	for player in map_players.values():
		for big in player.map_bigs.values():
			if big == null:
				continue
			big.updateJianVisible()


# 观战者视野刷新
func refreshViewForWatcher():
	if is_gameOver:
		return
	for point in map_points.values():
		point.is_view = false
	for player in map_players.values():
		var playerr = player as Player
		if playerr.alive:
			me_player = playerr
			var arr_points = GameUtils.getArrPointsHasView()
			for point in arr_points:
				point.is_view = true
	me_player = null
	for point in map_points.values():
		point.refreshView()
	# 大子显示刷新
	for player in map_players.values():
		for big in player.map_bigs.values():
			if big == null:
				continue
			big.updateVisible()
	# 大子的键显示刷新
	for player in map_players.values():
		for big in player.map_bigs.values():
			if big == null:
				continue
			big.updateJianVisible()

#玩家回合切换
func switchTurnPlay(data:Dictionary):
	$UI.closeAll()
	turn_player = map_players.get(data.username) as Player
	turn_player.turnStart()
	$"UI/回合控制".refresh(turn_player)
	if ObjectManager.gameManager.me_player == null || turn_player.is_me:
		ObjectManager.toast.showToast("你 的 回 合")
		ObjectManager.soundManager.play(SoundType.turn_change)

# 播放动画,有音效一块儿就播放了
func play_effect(data:Dictionary):
	var effect = data.effect
	var point = map_points.get(data.point_id)
	var player = map_players.get(data.username)
	match effect:
		"big_die":
			layer_effect.bigDie(point, player, true)
		"big_毒爆":
			layer_effect.poisonousExplosion(point)
		"妖术_冰冻":
			layer_effect.妖术_冰冻(point)
		"妖术_瘟疫":
			layer_effect.妖术_瘟疫(point)

# 单独播放音效
func play_sound(data:Dictionary):
	var sound = data.sound
	var player = map_players.get(data.username) as Player
	if player.is_me || me_player == null:
		match sound:
			"恶灵_附身":
				ObjectManager.soundManager.play(SoundType.恶灵_附身)
			"鼓舞_鼓舞":
				ObjectManager.soundManager.play(SoundType.鼓舞_鼓舞)
			"精灵_祝福":
				ObjectManager.soundManager.play(SoundType.精灵_祝福)
			"沙兵_化沙":
				ObjectManager.soundManager.play(SoundType.沙兵_化沙)
			"天谕_眨眼":
				ObjectManager.soundManager.play(SoundType.天谕_眨眼)
			"妖术_献祭":
				ObjectManager.soundManager.play(SoundType.妖术_献祭)
			"野猪_冲锋":
				ObjectManager.soundManager.play(SoundType.野猪_冲锋)


# 自动刷新视野
func _on_timer_timeout():
	if me_player && me_player.alive:
		refreshView()
	else :
		refreshViewForWatcher()

func gameOver(data:Dictionary):
	is_gameOver = true
	operationManager.is_can_click_point = false
	operationManager.is_can_control_camera = false
	ui.gameOver(data.winner)
